﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class EmployeeList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F2652;
        protected GridView gvEmployee;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblEmployee;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2EmployeeList(EmployeeQueryEntity employeeQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(employeeQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(employeeQuery, "EMPLOYEE");
            base.BindData2GridView(this.gvEmployee, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvEmployee, "EMPLOYEE_ID");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(selectedID, "EMPLOYEE") + base.GetGlobalMsgResource("C006");
                    EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
                    this.BindData2EmployeeList(employeeQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvEmployee, "EMPLOYEE_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("Employee.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
            employeeQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = employeeQuery.CurrentPage;
            employeeQuery.CurrentPage = "0";
            this.gvEmployee.Columns[1].Visible = false;
            this.BindData2EmployeeList(employeeQuery);
            this.gvEmployee.ShowFooter = false;
            employeeQuery.IsGetAll = false;
            employeeQuery.CurrentPage = currentPageIndex;
            this.SetEmployeeQueryEntity(employeeQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("Employee.xls", this.gvEmployee);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
            employeeQuery.CurrentPage = "0";
            this.BindData2EmployeeList(employeeQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
            employeeQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2EmployeeList(employeeQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
            employeeQuery.EmployeeID = "";
            employeeQuery.EmployeeName = "";
            employeeQuery.Position = "";
            employeeQuery.DepartmentID = "";
            employeeQuery.EntrantDate = "";
            employeeQuery.CreatedBy = "";
            employeeQuery.CreatedDate = "";
            this.BindData2EmployeeList(employeeQuery);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
            string type = "";
            if ((employeeQuery != null) && !string.IsNullOrEmpty(employeeQuery.Position))
            {
                type = employeeQuery.Position.Replace("LIKE", "").Replace("%", "").Replace("'", "").Trim();
            }
            base.Server.Transfer(string.Format("EmployeeQuery.aspx?type={0}&action={1}&from=list", type, this.hdnActionID.Value.Trim()));
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
            if (int.Parse(employeeQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                employeeQuery.CurrentPage = (int.Parse(employeeQuery.CurrentPage) + 1).ToString();
                this.BindData2EmployeeList(employeeQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
            if (int.Parse(employeeQuery.CurrentPage) > 0)
            {
                employeeQuery.CurrentPage = (int.Parse(employeeQuery.CurrentPage) - 1).ToString();
                this.BindData2EmployeeList(employeeQuery);
            }
        }

        private EmployeeQueryEntity GetEmployeeQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as EmployeeQueryEntity);
            }
            return (base.PageQueryEntitySession as EmployeeQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
                employeeQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2EmployeeList(employeeQuery);
            }
        }

        protected void gvEmployee_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2652", this.gvEmployee);
        }

        protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvEmployee_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvEmployee.DataKeys[e.NewSelectedIndex]["EMPLOYEE_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvEmployee_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                EmployeeQueryEntity employeeQuery = this.GetEmployeeQueryEntity();
                employeeQuery.SortField = e.SortExpression;
                if (employeeQuery.SortDirection == "ASC")
                {
                    employeeQuery.SortDirection = "DESC";
                }
                else if (employeeQuery.SortDirection == "DESC")
                {
                    employeeQuery.SortDirection = "ASC";
                }
                this.BindData2EmployeeList(employeeQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            EmployeeQueryEntity employeeQuery = new EmployeeQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    employeeQuery.EmployeeID = this.PreviousPage.EmployeeID;
                    employeeQuery.EmployeeName = this.PreviousPage.EmployeeName;
                    employeeQuery.Position = this.PreviousPage.Position;
                    employeeQuery.EmployeeID = this.PreviousPage.EmployeeID;
                    employeeQuery.EntrantDate = this.PreviousPage.EntrantDate;
                    employeeQuery.CreatedBy = this.PreviousPage.CreatedBy;
                    employeeQuery.CreatedDate = this.PreviousPage.CreatedDate;
                }
                else
                {
                    employeeQuery.EmployeeID = "";
                    employeeQuery.EmployeeName = "";
                    employeeQuery.Position = "";
                    employeeQuery.EmployeeID = "";
                    employeeQuery.EntrantDate = "";
                    employeeQuery.CreatedBy = "";
                    employeeQuery.CreatedDate = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvEmployee.Columns[1].Visible = false;
                this.gvEmployee.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnAction.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
                this.btnList.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            employeeQuery.IsGetAll = false;
            employeeQuery.CurrentPage = "0";
            employeeQuery.PageSize = this.gvEmployee.PageSize.ToString();
            employeeQuery.SortField = "E.ROWID";
            employeeQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvEmployee.PageSize;
            this.SetEmployeeQueryEntity(employeeQuery);
            this.BindData2EmployeeList(employeeQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetEmployeeQueryEntity(EmployeeQueryEntity employeeQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = employeeQuery;
            }
            else
            {
                base.PageQueryEntitySession = employeeQuery;
            }
        }

        public EmployeeQuery PreviousPage
        {
            get
            {
                return (EmployeeQuery) base.PreviousPage;
            }
        }
    }
}

